package org.mtmi.ui.controls.edits;

import java.util.Collection;

import org.eclipse.swt.graphics.Rectangle;
import org.mtmi.ui.IMG;
import org.mtmi.ui.actions.RetargetableAction;
import org.mtmi.ui.controls.IOrientable;
import org.mtmi.ui.controls.IVisualControl;
import org.mtmi.ui.drawables.IDrawable;

public class ToggleOrientationAction extends RetargetableAction
{
	
	public ToggleOrientationAction() {
		super( PUSH );
	}

	@Override
	public boolean isEnabled() { return true; }
	
	@Override
	public boolean isTargetHandled( Object c ) {
		return isInstanceOfOrCollectionOfInstanceOf( c, IOrientable.class ); 
	}
	
	/** -1: don't know */
	private int getOrientation() {
		Object t = getTarget();
		if( t instanceof IOrientable ) {
			return ((IOrientable)t).isHorizontal() ? 1 : 0;
		}
		else {
			int or=-1;
			for( Object o : (Collection<?>)t ) {
				int v = ((IOrientable)t).isHorizontal() ? 1 : 0;
				if( or<0 ) {
					or=v;
				} else if( or!=v ) {
					return -1;
				}
			}
			return or;
		}
	}
	
	@Override
	public IDrawable getImage() {
		return IMG.GetD( IMG.I_ORIENTATION );
	}
	
	@Override
	public void run() {
		Object t = getTarget();
		if( t instanceof IOrientable ) {
			run( (IOrientable)t );
		}
		else {
			for( Object o : (Collection<?>)t ) {
				run( (IOrientable)o );
			}
		}
	}
	
	private void run( IOrientable o ) {
		o.setHorizontal( !o.isHorizontal() );
	}
}
